import { CustomComponent } from '/engine/index';

CustomComponent({
  options: {},
  properties: {
    title: {
      type: String,
      value: '默认标题',
    },
    showBack: {
      type: Boolean,
      value: true,
    },
    onClickBack: {
      type: Function,
      value: null,
    },
    isChatNav: {
      type: Boolean,
      value: false,
    },
    unreadCount: {
      type: Number,
      value: 0,
    },
  },
  data: {
    titleTop: 0,
    navHeight: 0,
  },
  lifetimes: {
    attached() {
      const menuButtonInfo = wx.getMenuButtonBoundingClientRect();

      this.setData({
        navHeight: menuButtonInfo.height,
        titleTop: menuButtonInfo.top,
      });
    },
  },
  methods: {
    onNavigateToMine() {
      wx.navigateTo({ url: '/pages/mine/index' });
    },
    onTapBack() {
      const pages = getCurrentPages();
      // 查找路由堆栈中是否已有 mine 页面
      let minePageIndex = -1;
      for (let i = 0; i < pages.length - 1; i++) {
        if (pages[i].route === 'pages/mine/index') {
          minePageIndex = i;
          break;
        }
      }

      if (minePageIndex !== -1) {
        // 如果找到了 mine 页面，计算需要返回的层数
        const delta = pages.length - 1 - minePageIndex;
        wx.navigateBack({ delta });
      } else {
        // 如果没有找到 mine 页面，则重定向到 mine 页面
        wx.redirectTo({ url: '/pages/mine/index' });
      }
    },
  },
});
